package com.HuiShou.controller.collection;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.HuiShou.common.utils.LocationUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.HuiShou.common.annotation.Log;
import com.HuiShou.common.core.controller.BaseController;
import com.HuiShou.common.core.domain.AjaxResult;
import com.HuiShou.common.enums.BusinessType;
import com.HuiShou.domain.CollectorOrder;
import com.HuiShou.service.ICollectorOrderService;
import com.HuiShou.common.utils.poi.ExcelUtil;
import com.HuiShou.common.core.page.TableDataInfo;

/**
 * 接取订单Controller
 * 
 * @author lyz
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/order/collectorOrder")
public class CollectorOrderController extends BaseController
{
    @Autowired
    private ICollectorOrderService collectorOrderService;

    /**
     * 查询接取订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:collectorOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(CollectorOrder collectorOrder)
    {
        // 先查询总数
        List<CollectorOrder> allList = collectorOrderService.selectCollectorOrderList(collectorOrder);
        int total = allList.size();
        
        // 再分页查询
        startPage();
        List<CollectorOrder> list = collectorOrderService.selectCollectorOrderList(collectorOrder);
        
        // 计算距离并排序
        list.forEach(e->e.setDistance(LocationUtil.getDistance(collectorOrder.getUserLongitude(),collectorOrder.getUserLatitude(),e.getLongitude(),e.getLatitude())));

        // 使用nullsLast处理null值
        list = list.stream()
                .sorted(Comparator.comparing(
                        CollectorOrder::getDistance,
                        Comparator.nullsLast(Comparator.naturalOrder())
                ))
                .collect(Collectors.toList());
        
        TableDataInfo rspData = getDataTable(list);
        // 使用正确的总数
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 导出接取订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:collectorOrder:export')")
    @Log(title = "接取订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CollectorOrder collectorOrder)
    {
        List<CollectorOrder> list = collectorOrderService.selectCollectorOrderList(collectorOrder);
        ExcelUtil<CollectorOrder> util = new ExcelUtil<CollectorOrder>(CollectorOrder.class);
        util.exportExcel(response, list, "接取订单数据");
    }

    /**
     * 获取接取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:collectorOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(collectorOrderService.selectCollectorOrderByOrderId(orderId));
    }

    /**
     * 新增接取订单
     */
    @PreAuthorize("@ss.hasPermi('order:collectorOrder:add')")
    @Log(title = "接取订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CollectorOrder collectorOrder)
    {
        return toAjax(collectorOrderService.insertCollectorOrder(collectorOrder));
    }

    /**
     * 修改接取订单
     */
    @PreAuthorize("@ss.hasPermi('order:collectorOrder:edit')")
    @Log(title = "接取订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CollectorOrder collectorOrder)
    {
        return toAjax(collectorOrderService.updateCollectorOrder(collectorOrder));
    }

    /**
     * 完成接取订单
     */
    @PreAuthorize("@ss.hasPermi('order:collectorOrder:remove')")
    @Log(title = "接取订单", businessType = BusinessType.DELETE)
	@PutMapping("/finish")
    public AjaxResult remove(@RequestBody CollectorOrder collectorOrder)
    {
        return toAjax(collectorOrderService.finishCollectorOrder(collectorOrder));
    }
}
